<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String change_id = request.getParameter("change_id") == null ? "" : request.getParameter("change_id");
	boolean isAdd = "".equals(change_id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建预算调整":"编辑预算调整" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
	var m_lastIndex=-1;
	var combogridTarget;
	function loadMaterial(){
		//----新增材料预算
		//注册combogrid
		jQuery.extend(jQuery.fn.datagrid.defaults.editors, {
			   combogrid: {
				    init: function(container, options){
					    var input = jQuery('<input  type="text" class="datagrid-editable-input">').appendTo(container);
					   	input.combogrid(options);
					    return input;
			    	},
			    	distroy:function(target){
			    		jQuery(target).combogrid('distroy');
			    	},
				    getValue: function(target){
				    	return jQuery(target).combogrid('getValue');
				    },
				    setValue: function(target, value){
				   	 	jQuery(target).combogrid('setValue',value);
				    },
				    resize: function(target, width){
				    	combogridTarget = target;
					    jQuery(target).combogrid('resize',width);
				    }
			    }
			 });
		
			jQuery("#mSearchValue").keypress(function(e) { 
			// 回车键事件 
				if(e.which == 13) { 
					 domSearch();
			   	} 
				}); 
			jQuery("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					domSearch();
				}
			});
			jQuery('#mtb').hide();
			
			jQuery('#material').datagrid({
			  	title:'材料预算',
			  	iconCls:'icon-edit',
			  	<%=isAdd?"url:'"+contextPath+"/springMVC/gd/listPageMaterial?gd_id="+gd_id+"',":"url:'"+contextPath+"/springMVC/gd/listPageChangeMaterial?change_id="+change_id+"',"%>
			  	rownumbers:true,
			  	singleSelect:true,
				fitColumns:true,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'材料id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'材料编号',field:'code',width:100,align:'center',editor:{
						type:'combogrid',
						options:{
							url: '<%=contextPath %>/springMVC/warehouse/listPageMaterial',
						    panelWidth:750,
						    panelHeight:355,
						    rownumbers:true,
						    fitColumns:true,
						    columns: [[  
						       {field:'id',title:'主键',hidden:true},  
						       {field:'material_type_name',title:'材料类别',width:100,align:'center'},
						       {field:'code',title:'材料编号',width:100,align:'center'},
						       {field:'name',title:'材料名称',width:150,align:'center'},
						       {field:'format_type',title:'规格/型号',width:100,align:'center'},
						       {field:'unit_name',title:'单位',width:50,align:'center'}
						    ]],
						    onBeforeLoad:function(param){
								var mSearchValue = jQuery('#mSearchValue').val();
								var material_type_id = jQuery('#material_type_id').combotree('getValue');
								param.searchValue=mSearchValue;
								param.material_type_id=material_type_id;
							},
						    toolbar:'#mtb',
						    pageList : [10,50,100],  
							pagination:true,
							onClickRow:function(rowIndex,rowData){
								if(checkSameProduct1(rowData,jQuery('#material').datagrid('getData').rows)){
									var pRow = jQuery('#material').datagrid('getRows')[m_lastIndex];
									if(pRow.num != '0'){
										$.messager.alert("提示","该材料已经包含预算值，不能修改！");
										jQuery('#material').datagrid('rejectChanges');
										m_lastIndex = -1;
										return;
									}
									pRow.material_id = rowData.id;
									pRow.code = rowData.code;
									pRow.name = rowData.name;
									pRow.unit_name = rowData.unit_name;
									pRow.format_type = rowData.format_type;
									jQuery('#material').datagrid('getData').rows[m_lastIndex]=pRow;
									var data = jQuery('#material').datagrid('getData');
									jQuery('#material').datagrid('loadData',data);
									
									jQuery('#material').datagrid('endEdit', m_lastIndex);
									jQuery('#material').datagrid('beginEdit', m_lastIndex);
								}else{
									jQuery.messager.alert("提示","选择的材料已经存在");
									jQuery('#material').datagrid('rejectChanges');
									m_lastIndex = -1;
								}
							}
						}
					}},
					{title:'材料名称',field:'name',width:100,align:'center'},
					{title:'规格/型号',field:'format_type',width:100,align:'center'},
					{title:'单位',field:'unit_name',width:50,align:'center'},
					{title:'之前预算数量',field:'num',width:100,align:'center'},
					{title:'已使用数量',field:'already_use',width:100,align:'center'},
					{title:'审批占用数',field:'queue_num',width:100,align:'center'},
					{title:'之前备注',field:'remark',width:100,align:'center'},
					{title:'调整预算数量',field:'after_num',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							min:0,
							precision:2
						}
					},formatter:function(value,row,index){
						<%if(isAdd){%>
						if(value==undefined){
							row.after_num = row.num;
							return row.after_num;
						}
						<%}%>
						return value;
					}},
					{title:'调整备注',field:'after_remark',width:100,align:'center',editor:{
						type:'textarea'
					},formatter:function(value,row,index){
						<%if(isAdd){%>
						if(value==undefined){
							row.after_remark = ''; 
							return row.after_remark;
						}
						<%}%>
						return value;
					}}
				]],
				onBeforeLoad:function(){
					jQuery(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (m_lastIndex != rowIndex){
						jQuery('#material').datagrid('endEdit', m_lastIndex);
						jQuery('#material').datagrid('beginEdit', rowIndex);
						var ed = jQuery('#material').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = jQuery('#material').datagrid('getEditor', {index:rowIndex,field:'after_num'});
						}
						jQuery(ed.target).select();
					}
					m_lastIndex = rowIndex;
				},
				pageSize:10,
				pageList : [10,50,100],  
				pagination:true,
				toolbar:[
					{
						id:'add',
						text:'添加材料',
						disabled:false,
						iconCls:'icon-add',
						handler:function(){
							addMRow();
						}
					},'-',{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						jQuery('#material').datagrid('acceptChanges');
						m_lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						deleteMRow();
					}
				}
			   ]
			});
	}
	function addMRow() {
		jQuery('#material').datagrid('endEdit', m_lastIndex);
		jQuery('#material').datagrid('appendRow', {
			already_use:'0',
			queue_num:'0',
			remark:'',
			num:'0',
			after_remark:''			
		});
		var data = jQuery('#material').datagrid('getData');
		jQuery('#material').datagrid('loadData', data);
		m_lastIndex = jQuery('#material').datagrid('getRows').length - 1;
		jQuery('#material').datagrid('selectRow', m_lastIndex);
		jQuery('#material').datagrid('beginEdit', m_lastIndex);
	}
	function deleteMRow() {
		jQuery('#material').datagrid('acceptChanges');
		m_lastIndex = -1;
		var rows = jQuery('#material').datagrid('getSelections');
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				var row = rows[i];
				var index = jQuery('#material').datagrid('getRowIndex', row);
				if(row.already_use != '0' || row.queue_num != '0'){
					$.messager.alert("提示","第"+(index+1)+"行材料已经被使用或被审批占用，不能删除！");
					return;
				}
				jQuery('#material').datagrid('deleteRow', index);
			}
		}
		var data = jQuery('#material').datagrid('getData');
		jQuery('#material').datagrid('loadData', data);
	}
	function domSearch() {
		var mSearchValue = jQuery('#mSearchValue').val();
		var material_type_id = jQuery('#material_type_id').combotree('getValue');
		jQuery(combogridTarget).combogrid('grid').datagrid('load', {
			searchValue : mSearchValue,
			material_type_id : material_type_id
		});
	}
	
	var fb_lastIndex=-1;
	function loadFB(){
		$('#fb_detail').datagrid({
		  	title:'分包预算',
		  	iconCls:'icon-edit',
		  	<%=isAdd?"url:'"+contextPath+"/springMVC/gd/listPageFBDetail?gd_id="+gd_id+"',":"url:'"+contextPath+"/springMVC/gd/listPageChangeFBDetail?change_id="+change_id+"',"%>
		  	rownumbers:true,
		  	singleSelect:true,
			fitColumns:true,
			frozenColumns:[[
				{field:'ck',checkbox:true},
			    {title:'分包栏目id',field:'fb_item_id',hidden:true}
			]],
			columns:[[
				{title:'栏目名称',field:'fb_item_name',width:50,align:'center',editor:{
					type:'combobox',
					options:{
					    url:'<%=contextPath%>/springMVC/group/getFBItem',  
					    valueField:'id',  
					    textField:'name',
					    onHidePanel:function(){
					    	var pRow = jQuery('#fb_detail').datagrid('getRows')[fb_lastIndex];
							if(pRow.budget != '0'){
								$.messager.alert("提示","该栏目已经有分包值，不能修改栏目！");
								jQuery('#fb_detail').datagrid('rejectChanges');
								fb_lastIndex = -1;
								return;
							}
							pRow.fb_item_id = jQuery(this).combobox('getValue');
							pRow.fb_item_name = jQuery(this).combobox('getText');
							jQuery('#fb_detail').datagrid('getData').rows[fb_lastIndex]=pRow;
							var data = jQuery('#fb_detail').datagrid('getData');
							jQuery('#fb_detail').datagrid('loadData',data);
							
							jQuery('#fb_detail').datagrid('endEdit', fb_lastIndex);
							jQuery('#fb_detail').datagrid('beginEdit', fb_lastIndex);
					    }
					}
				}},
				{title:'之前预算成本',field:'budget',width:50,align:'center'},
				{title:'预算备注',field:'budget_remark',width:100,align:'center'},
				{title:'调整预算成本',field:'after_budget',width:50,align:'center',editor:{
					type:'numberbox',
					options:{
						precision:2
					}
				},formatter:function(value,row,index){
					<%if(isAdd){%>
					if(value==undefined){
						row.after_budget = row.budget;
						return row.after_budget;
					}
				<%}%>
				return value;
			}},
				{title:'调整预算备注',field:'after_budget_remark',width:100,align:'center',editor:{
					type:'textarea'
				},formatter:function(value,row,index){
					<%if(isAdd){%>
					if(value==undefined){
						row.after_budget_remark = ''; 
						return row.after_budget_remark;
					}
					<%}%>
					return value;
				}}
			]],
			onBeforeLoad:function(){
				jQuery(this).datagrid('rejectChanges');
			},
			onClickCell:function(rowIndex,field,value){
				if (fb_lastIndex != rowIndex){
					jQuery('#fb_detail').datagrid('endEdit', fb_lastIndex);
					jQuery('#fb_detail').datagrid('beginEdit', rowIndex);
					var ed = jQuery('#fb_detail').datagrid('getEditor', {index:rowIndex,field:field});
					if(ed == null){
						ed = jQuery('#fb_detail').datagrid('getEditor', {index:rowIndex,field:'after_budget'});
					}
					jQuery(ed.target).select();
				}
				fb_lastIndex = rowIndex;
			},
			pageSize:10,
			pageList : [10,50,100],  
			pagination:true,
			toolbar:[
				{
					id:'add',
					text:'添加预算栏目',
					disabled:false,
					iconCls:'icon-add',
					handler:function(){
						addFBRow();
					}
				},'-',{
				id:'save',
				text:'保存修改',
				disabled:false,
				iconCls:'icon-save',
				handler:function(){
					jQuery('#fb_detail').datagrid('acceptChanges');
					fb_lastIndex =-1;
				}
				},'-',{
				id:'delete',
				text:'删除',
				disabled:false,
				iconCls:'icon-remove',
				handler:function(){
					deleteFBRow();
				}
			}
		   ]
		});
	}	
	
	function addFBRow() {
		jQuery('#fb_detail').datagrid('endEdit', fb_lastIndex);
		jQuery('#fb_detail').datagrid('appendRow', {
			fb_item_name : '',
			budget : '0',
			budget_remark : '',
			after_budget : '',
			after_budget_remark : ''
		});
		var data = jQuery('#fb_detail').datagrid('getData');
		jQuery('#fb_detail').datagrid('loadData', data);
		fb_lastIndex = jQuery('#fb_detail').datagrid('getRows').length - 1;
		jQuery('#fb_detail').datagrid('selectRow', fb_lastIndex);
		jQuery('#fb_detail').datagrid('beginEdit', fb_lastIndex);
	}
	
	function deleteFBRow() {
		jQuery('#fb_detail').datagrid('acceptChanges');
		fb_lastIndex = -1;
		var rows = jQuery('#fb_detail').datagrid('getSelections');
		if (rows.length > 0) {
			for ( var i = 0; i < rows.length; i++) {
				var row = rows[i];
				var index = jQuery('#fb_detail').datagrid('getRowIndex', row);
				jQuery('#fb_detail').datagrid('deleteRow', index);
			}
		}
		var data = jQuery('#fb_detail').datagrid('getData');
		jQuery('#fb_detail').datagrid('loadData', data);
	}
	
		$(function(){
			loadFB();
			loadMaterial();
			<%if(isAdd){%>
			jQuery.ajax({
				type:"POST",
				url:"<%=contextPath %>/springMVC/gd/getGDControl?gd_id=<%=gd_id%>",
				success:function(rtData){
					jQuery('#before_control').val(rtData);
				}
			});
			<%}else{%>
			jQuery.ajax({
				type:"POST",
				url:"<%=contextPath %>/springMVC/gd/getGDChangeControl?change_id=<%=change_id%>",
				success:function(rtData){
					jQuery('#before_control').val(rtData.before_control);
					jQuery('#after_control').numberbox('setValue',rtData.after_control);
				}
			});
			<%}%>
		});
		function submitForm(obj){
			jQuery('#fb_detail').datagrid('acceptChanges');
			fb_lastIndex =-1;
		  	jQuery('#material').datagrid('acceptChanges');
			m_lastIndex =-1;
			
			//分包预算
			  var details="";
			  var jsondata=jQuery("#fb_detail").datagrid("getData");
			  for(var i=0;i<jsondata.total;i++){
				 if(details!=""){
					 details+=";";
				 }
				 var index = jQuery("#fb_detail").datagrid("getRowIndex",jsondata.rows[i])+1;
				 if(jsondata.rows[i].fb_item_name == null || jsondata.rows[i].fb_item_name == ""){
					jQuery.messager.alert('提示','分包预算第'+index+'行未选择分包栏目！');
					return ;
				 }else if(jsondata.rows[i].after_budget == null || jsondata.rows[i].after_budget <= 0){
					jQuery.messager.alert('提示','分包预算第'+index+'行预算成本填写不正确！');
					return ;
				 }
				 details+=jsonToStr(jsondata.rows[i]);
			  }
			  if(details == ""){
				  jQuery.messager.alert('提示','分包预算还没填写！');
					return ;
			  }
			  details = encodeURIComponent(details);
			  
			var before_control = jQuery('#before_control').val();
			var after_control = jQuery('#after_control').numberbox('getValue');
			if(after_control == '' || after_control == null || after_control == 0){
				jQuery.messager.alert('提示','请重新填写管控进度');
				return;
			}
			  //材料预算
			  var materials="";
			  var jsondata=jQuery("#material").datagrid("getData");
			  for(var i=0;i<jsondata.total;i++){
				 if(materials!=""){
					 materials+=";";
				 }
				 var index = jQuery("#material").datagrid("getRowIndex",jsondata.rows[i])+1;
				 if(jsondata.rows[i].name == null || jsondata.rows[i].name == ""){
					jQuery.messager.alert('提示','材料预算第'+index+'行未选择材料！');
					return ;
				 }else if(jsondata.rows[i].after_num == null || jsondata.rows[i].after_num <= 0){
					jQuery.messager.alert('提示','材料预算第'+index+'行数量填写不正确！');
					return ;
				 }
				 materials+=jsonToStr(jsondata.rows[i]);
			  }
			  if(materials == ""){
				  jQuery.messager.alert('提示','材料预算还没填写！');
					return ;
			  }
			  materials = encodeURIComponent(materials);
			  var param = "gd_id=<%=gd_id%>&before_control="+before_control+"&after_control="+after_control+"&details="+details+"&materials="+materials;
			  $(obj).hide();
			  $.ajax({
				  type:'POST',
				  <%=isAdd?"url:'"+contextPath+"/springMVC/gd/addGDChangeAssume',":"url:'"+contextPath+"/springMVC/gd/updateGDChangeAssume?change_id="+change_id+"',"%>				
				  data:param,
				  success:function(rtJson){
					if (rtJson.rtState == '0'){
						eval('window.opener.<%=callback%>();');
						window.close();
					}else{
						warningInfo(rtJson.rtMsrg);
					}
				}
			  });
			  $(obj).show();
		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td colspan="4">
						<div id="fb_detail"></div>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>当前材料管控：</td>
					<td class="td2" nowrap >
						<input id="before_control" name="before_control" readOnly /> %
					</td>
					<td align="right" class="td1" nowrap>调整材料管控：</td>
					<td class="td2" nowrap >
						<input id="after_control" name="after_control" class="easyui-numberbox" data-options="precision:2,required:true" /> %
					</td>
				</tr>
				<tr>
					<td colspan="4">
						<div id="material"></div>
					</td>
				</tr>
			   <%if(!"1".equals(isShow)){ %>
				<tr>
					<td class="td2" nowrap colspan='4'>
						<div align="center">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="submitForm(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<%} %>
			</table>
		</div>
		<div id="mtb" style="padding:3px;height:auto">
				<div align="right">
					分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
					查询条件：<input id="mSearchValue" name="mSearchValue" style="width:150px" title="材料编码或名称"/>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="domSearch();"></a>
				</div>
			</div>
	</body>
</html>